package com.amor.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amor.R;
import com.amor.bean.MenuBean;
import com.amor.utils.Constant;
import com.amor.utils.LogUtil;

import java.util.ArrayList;

/**
 * Created by zcy on 2017/5/14 0014.
 */

public class MenuAdapter extends BaseAdapter {
    ArrayList<MenuBean> datas;
    Context context;
    LayoutInflater mInflater;

    public MenuAdapter(Context context, ArrayList<MenuBean> datas) {
        this.datas = datas;
        LogUtil.logD(Constant.TAG, "data size = " + datas.size());
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = mInflater.inflate(R.layout.item_menu, null);
            vh.iv = (ImageView) view.findViewById(R.id.item_iv);
            vh.name = (TextView) view.findViewById(R.id.item_name);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        vh.iv.setBackgroundResource(datas.get(i).getImgId());
        vh.name.setText(datas.get(i).getName());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtil.logI(Constant.TAG, "点击了" + i);
                try {
                    Intent menu = new Intent(context, Class.forName(datas.get(i).getClassName()));
                    menu.putExtra("tittle", datas.get(i).getName());
                    context.startActivity(menu);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return view;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ViewHolder {

        TextView name;
        ImageView iv;

    }

}
